package sortalg

import "fmt"

/**
直接插入排序
*/
type DirectInsertSort struct {
	keyList []int
}

func NewDirectInsertSort(sourceList []int) *DirectInsertSort {
	dis := &DirectInsertSort{
		keyList: sourceList,
	}

	return dis.insertSort()
}

func (dis *DirectInsertSort) insertSort() *DirectInsertSort {
	for i, v := range dis.keyList {
		if i == 0 {
			continue
		}

		j := i - 1
		for ; j >= 0 && dis.keyList[j] > v; {
			dis.keyList[j+1] = dis.keyList[j]
			j--
		}
		dis.keyList[j+1] = v
	}
	return dis
}

func (dis *DirectInsertSort) Print() {
	fmt.Println(dis.keyList)
}
